<?php
/**
 * Created by PhpStorm.
 * User: DY
 * Date: 2017/3/10
 * Time: 15:58
 */
namespace App\Http\Controllers;
require_once ('../app/Http/SDK/mns-autoloader.php');

use App\HTTP\Controllers\Controller;
use Mts\Request\V20140618 as Mts;
use Sts\Request\V20150401 as Sts;
use OSS\OssClient;
use App\Http\Common\OSSConfig;
use App\Common\Common;


use AliyunMNS\Client;
use AliyunMNS\Topic;
use AliyunMNS\Constants;
use AliyunMNS\Model\MailAttributes;
use AliyunMNS\Model\SmsAttributes;
use AliyunMNS\Model\BatchSmsAttributes;
use AliyunMNS\Model\MessageAttributes;
use AliyunMNS\Exception\MnsException;
use AliyunMNS\Requests\PublishMessageRequest;

class OSSController extends Controller
{

    var $ossClient = null;
    var $client = null;
    var $accessKeyID = null; // 替换成真实的id
    var $accessKeySecret = null; // 替换成真实的secret
    var $endpoint_hz = 'cn-hangzhou';
    var $endpoint_bj = 'cn-beijing';
    var $smsurl = 'http://sms.aliyuncs.com?';




    /**获取临时授权DY
     * @return array
     * @throws \ClientException
     */



    public function run($mobile,$code)
    {
        /**
         * Step 1. 初始化Client
         */
        $this->endPoint = "http://1977680567921435.mns.cn-hangzhou.aliyuncs.com/"; // eg. http://1234567890123456.mns.cn-shenzhen.aliyuncs.com
        $this->accessId = "LTAIZwZEnzqRP0nP";
        $this->accessKey = "IdtkdE8F8S8xKBZMScvliW3mCWbAGR";

        $this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);
        /**
         * Step 2. 获取主题引用
         */
        $topicName = "sms.topic-cn-hangzhou";
        $topic = $this->client->getTopicRef($topicName);
        /**
         * Step 3. 生成SMS消息属性
         */
        // 3.1 设置发送短信的签名（SMSSignName）和模板（SMSTemplateCode）
        $batchSmsAttributes = new BatchSmsAttributes("牛魔科技", "SMS_70265262");
        // 3.2 （如果在短信模板中定义了参数）指定短信模板中对应参数的值
        $batchSmsAttributes->addReceiver("$mobile", array("code" => "$code","product"=>"牛魔科技"));
        $messageAttributes = new MessageAttributes(array($batchSmsAttributes));
        /**
         * Step 4. 设置SMS消息体（必须）
         *
         * 注：目前暂时不支持消息内容为空，需要指定消息内容，不为空即可。
         */
        $messageBody = "smsmessage";
        /**
         * Step 5. 发布SMS消息
         */
        $request = new PublishMessageRequest($messageBody, $messageAttributes);
        try
        {
            $res = $topic->publishMessage($request);
            return $res;
        }
        catch (MnsException $e)
        {
            return $e;
        }
    }


}

?>